﻿namespace SuperMarieApi.App.Controllers.Framework.Common
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    public static class EnumHelper
    {
        public static Dictionary<int, string> GetEnumList(Type enumType)
        {
            var fields = enumType.GetFields();
            Dictionary<int, string> result = new Dictionary<int, string>();
            foreach (var item in fields)
            {
                var des=(DescriptionAttribute[])item.GetCustomAttributes(typeof(DescriptionAttribute),false);
                if (des.Length > 0)
                {
                    result.Add((int)item.GetValue(item.Name), des[0].Description);
                }
            }

            return result;
        }

        public static string GetEnumDescription(Enum value)
        {
            var fi = value.GetType().GetField(value.ToString());
            if (fi != null)
            {
                var des = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (des.Length > 0)
                {
                    return des[0].Description;
                }
            }
            return "";
        }
    }
}
